package com.example.daily.ui.main

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.example.daily.ui.navigation.BottomNavItem
import com.example.daily.ui.navigation.Screen
import com.example.daily.ui.foodrecord.FoodRecordScreen
import com.example.daily.ui.foodrecognition.FoodRecognitionScreen
import com.example.daily.ui.statistics.StatisticsScreen
import com.example.daily.ui.profile.ProfileScreen
import com.example.daily.ui.screen.AiConsultScreen
import com.example.daily.ui.camera.CameraScreen

@Composable
fun MainScreen(
    navController: NavHostController = rememberNavController()
) {
    Scaffold(
        bottomBar = {
            BottomNavigationBar(navController)
        }
    ) { paddingValues ->
        Box(modifier = Modifier.padding(paddingValues)) {
            MainNavGraph(navController)
        }
    }
}

@Composable
fun BottomNavigationBar(navController: NavHostController) {
    val items = listOf(
        BottomNavItem.FoodRecord,
        BottomNavItem.Statistics,
        BottomNavItem.AiConsult,
        BottomNavItem.Profile
    )
    
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentRoute = navBackStackEntry?.destination?.route
    
    NavigationBar {
        items.forEach { item ->
            NavigationBarItem(
                icon = { Icon(item.icon, contentDescription = item.title) },
                label = { Text(item.title) },
                selected = currentRoute == item.route,
                onClick = {
                    navController.navigate(item.route) {
                        popUpTo(navController.graph.findStartDestination().id) {
                            saveState = true
                        }
                        launchSingleTop = true
                        restoreState = true
                    }
                }
            )
        }
    }
}

@Composable
fun MainNavGraph(navController: NavHostController) {
    NavHost(
        navController = navController,
        startDestination = BottomNavItem.FoodRecord.route
    ) {
        composable(BottomNavItem.FoodRecord.route) {
            FoodRecordScreen(navController = navController)
        }
        composable(BottomNavItem.FoodRecognition.route) {
            FoodRecognitionScreen(navController = navController)
        }
        composable(BottomNavItem.Statistics.route) {
            StatisticsScreen()
        }
        composable(BottomNavItem.Profile.route) {
            ProfileScreen()
        }
        composable(BottomNavItem.AiConsult.route) {
            AiConsultScreen()
        }
        composable(Screen.Camera.route) {
            CameraScreen(
                navController = navController,
                onPhotoTaken = { }  // 照片会通过 savedStateHandle 传递
            )
        }
    }
} 